from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from Model.models import Package


def timeCycle(time: str):
    """
    日期转换
    :param time:
    :return:
    """
    month_dict = {
        "01": "Jan", "02": "Feb", "03": "Mar", "04": "Apr",
        "05": "May", "06": "Jun", "07": "Jul", "08": "Aug",
        "09": "Sept", "10": "Oct", "11": "Nov", "12": "Dec",
    }
    time_list = time.split(" ")
    date = time_list[0]
    data_list = date.split("-")
    data_list[1] = month_dict[data_list[1]]
    data_list.reverse()
    date = ' '.join(data_list)
    return date


def getEndDateByDurationAndPackage(packageId: int, startTime: str):
    """
    :param packageId 套餐ID
    :return duration
    """
    duraction = Package.objects.get(package_id=packageId).package_duration
    startDate = datetime.strptime(startTime, '%Y-%m-%d')
    if duraction > 1:
        month = int(duraction)
        endDate = startDate + relativedelta(months=month)
    else:
        days = int(float(duraction * 30))
        endDate = startDate + relativedelta(days=days)

    return endDate
